#pragma once
#include "Log.hpp"
#include <iostream>
#include <cassert>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <cstring>

using namespace std;

#define PATH_NAME "/home/wlh"
#define PROJ_ID 0x77
#define SHMSIZE 4096 // 4096的整数倍

#define FIFO_PATH "./fifo"
#define READ O_RDONLY
#define WRITE O_WRONLY

string TransKeyToHex(int k)
{
    char buffer[32] = {0};
    snprintf(buffer, sizeof(buffer), "0x%x", k);
    return buffer;
}

class Init
{
public:
    Init()
    {
        umask(0);
        int n = mkfifo(FIFO_PATH, 0666); // 创建管道
        assert(n != -1);
        Log("创建管道成功", Notice) << endl;
    }

    ~Init()
    {
        int n = unlink(FIFO_PATH); // 删除管道
        assert(n != -1);
        Log("删除管道成功", Notice) << endl;
    }
};

int openFIFO(int flags)
{
    int fd = open(FIFO_PATH, flags);
    Log("打开管道成功", Notice) << endl;
    assert(fd >= 0);
    return fd;
}

void closeFIFO(int fd)
{
    close(fd);
    Log("关闭管道成功", Notice) << endl;
}

void Wait(int fd)
{
    Log("等待中...", Notice) << endl;
    uint32_t tmp;
    size_t s = read(fd, &tmp, sizeof(uint32_t));
    assert(s == sizeof(uint32_t));
}

void signal(int fd)
{
    Log("唤醒中...", Notice) << endl;
    uint32_t tmp;
    size_t s = write(fd, &tmp, sizeof(uint32_t));
    assert(s == sizeof(uint32_t));
}
